package com.jinzhi.jzapi.certificate.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.ValidUtils;
import com.jinzhi.jzweb.domain.InfoCollectDO;
import com.jinzhi.jzweb.domain.InfoStoreDO;
import com.jinzhi.jzweb.domain.TrainStudentInfoDO;
import com.jinzhi.jzweb.service.InfoCollectService;
import com.jinzhi.jzweb.service.InfoStoreService;
import com.jinzhi.jzweb.service.TrainStudentInfoService;
import com.jinzhi.sys.domain.UserDO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

@Slf4j
@Controller
@RequestMapping("/jzApi/info")
public class ApiInfoController extends BaseController {

	@Autowired
	private InfoCollectService infoCollectService;
	@Autowired
    private InfoStoreService infoStoreService;


    @Log("提交表单")
    @ResponseBody
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    @PostMapping("/submitInfo")
    ApiResult<String> submitInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token,Long infoCollectId , Long infoStoreId,String content) {
        InfoCollectDO infoCollect=null;
        if(infoCollectId!=0){
            infoCollect = infoCollectService.selectById(infoCollectId);
        }
        UserDO user = this.getClientUser(token);
        InfoStoreDO infoStoreDO=infoStoreService.selectById(infoStoreId);

        if(infoStoreDO.getMaxNum()>0){
            Wrapper<InfoCollectDO> wrapper = new EntityWrapper<InfoCollectDO>();
            wrapper.eq("info_store_id",infoStoreId);
            wrapper.eq("status",1);

            int count = infoCollectService.selectCount(wrapper);
            if(count>=infoStoreDO.getMaxNum()){
                return ApiResult.fail("提交失败，该表单提交数量已达上线！");
            }
        }
        if(infoStoreDO.getMultiple()==0){
            InfoCollectDO info=infoCollectService.findOneByKv("user_id",user.getId(),"info_store_id",infoStoreId,"status","1");
            if(info!=null && info.getStatus()!=0 && info.getStatus()!=9){
                return ApiResult.fail("您已提交表单，请勿重复提交");
            }
        }
        if(infoCollect==null) {
            infoCollect = new InfoCollectDO();
            infoCollect.setContent(content);
            infoCollect.setInfoStoreId(infoStoreId);
            infoCollect.setSubmitDate(new Date());
            infoCollect.setStatus(1);
            infoCollect.setUserId(user.getId());
            infoCollectService.insert(infoCollect);
        }else{
            infoCollect.setContent(content);
            infoCollect.setInfoStoreId(infoStoreId);
            infoCollect.setSubmitDate(new Date());
            infoCollect.setStatus(1);
            infoCollectService.updateById(infoCollect);
        }
        return ApiResult.success();
    }

    @Log("保存表单草稿")
    @ResponseBody
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    @PostMapping("/saveInfo")
    ApiResult<String> saveInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token,Long infoCollectId, Long infoStoreId,String content) {
        InfoCollectDO infoCollect=null;
        if(infoCollectId!=0){
            infoCollect = infoCollectService.selectById(infoCollectId);
        }
        UserDO user = this.getClientUser(token);
        InfoStoreDO infoStoreDO=infoStoreService.selectById(infoStoreId);
        if(infoStoreDO.getMultiple()==0){
            InfoCollectDO info=infoCollectService.findOneByKv("user_id",user.getId(),"info_store_id",infoStoreId,"status","1");
            if(info!=null && info.getStatus()!=0 && info.getStatus()!=9){
                return ApiResult.fail("您已提交表单，请勿重复提交");
            }
        }
        if(infoCollect ==null) {
            infoCollect = new InfoCollectDO();
            infoCollect.setContent(content);
            infoCollect.setInfoStoreId(infoStoreId);
            infoCollect.setSubmitDate(new Date());
            infoCollect.setStatus(0);
            infoCollect.setUserId(user.getId());
            infoCollectService.insert(infoCollect);
        }else{
            infoCollect.setContent(content);
            infoCollect.setInfoStoreId(infoStoreId);
            infoCollect.setSubmitDate(new Date());
            infoCollect.setStatus(0);
            infoCollectService.updateById(infoCollect);
        }
        return ApiResult.success();
    }
}
